package app.domain.server.util;

import app.domain.order.Order;
import app.domain.server.Server;
import app.domain.util.MessageCreator;

public class MessageSorter {
	Server server;
	
	public MessageSorter(Server server) {
		this.server = server;
	}
	
	public void addMatchMessage(Order order, Order matchOrder, int amount, int amountLeft, String fillType) {
		synchronized(order.getClientId()) {
			server.getClients().get(order.getClientId()).getMessageOutBox().add(MessageCreator.match(order.getReferenceId(), matchOrder.getReferenceId(), amount, amountLeft, fillType,server.getMarketStockPrices().get(order.getTicket())));			
		}
	}

	public void addNewOrderMessage(Order order) {
		synchronized(order.getClientId()) {
			server.getClients().get(order.getClientId()).getMessageOutBox().add(MessageCreator.newOrder(order.getReferenceId(),
					order.getTicket(),order.getType(),order.getSide(),order.getAmount(),order.getPrice()));
		}
	}
	
	public void addCancelMessage(Order order) {
		synchronized(order.getClientId()) {
			server.getClients().get(order.getClientId()).getMessageOutBox().add(MessageCreator.cancel(order.getReferenceId()));			
		}
	}
	
	public void addNoCancelMessage(Order order) {
		synchronized(order.getClientId()) {
			server.getClients().get(order.getClientId()).getMessageOutBox().add(MessageCreator.cancel(order.getReferenceId()));			
		}
	}
	
}
